enum CacheType {
  SESSION,
  LOCAL
}

class Cache {
  storage: Storage
  constructor(type: CacheType = CacheType.LOCAL) {
    this.storage = type == CacheType.LOCAL ? localStorage : sessionStorage
  }
  setCache(key: string, value: any) {
    this.storage.setItem(key, JSON.stringify(value))
  }
  getCache(key: string) {
    let value = this.storage.getItem(key)
    if (value) {
      try {
        return JSON.parse(value)
      } catch (error) {
        return value
      }
    }else{
      return value
    }
  }
  removeCache(key: string) {
    this.storage.removeItem(key)
  }
  clearCache() {
    this.storage.clear() 
  }
}
const localCache = new Cache(CacheType.LOCAL)
const sessionCache = new Cache(CacheType.SESSION)
export {
  localCache,
  sessionCache
}